package com.example.mybatisflex.test.controller;

import com.example.mybatisflex.test.entity.Account;
import com.example.mybatisflex.test.mapper.AccountMapper;
import com.mybatisflex.core.audit.AuditManager;
import com.mybatisflex.core.audit.ConsoleMessageCollector;
import com.mybatisflex.core.audit.MessageCollector;
import com.mybatisflex.core.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
public class AccountController {

    @Resource
    private AccountMapper accountMapper;

    @GetMapping("/")
    List<Account> index() {
        return accountMapper.selectListByQuery(QueryWrapper.create().select().where("customer_name is not null").limit(10));
    }

    @GetMapping("/id/{id}")
    Account getById(@PathVariable Long id) {
        log.info("id={}", id);
        return accountMapper.selectOneById(id);
    }

    @GetMapping("/count")
    Long count() {
        return accountMapper.selectCountByQuery(QueryWrapper.create().select().where("customer_name is not null"));
    }

    @GetMapping("/save")
    @Transactional(rollbackFor = Exception.class)
    public void save() {
        Account account2 = new Account();
        account2.setCustomerName("用户");
        accountMapper.insert(account2);
    }
}
